package com.niodata.dp.monitor.sharelib.entity.file;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.niodata.dp.collection.StringList;
import com.niodata.dp.monitor.sharelib.view.file.FileAlarmView;
import java.util.ArrayList;
import java.util.Date;

public class FileAlarmInfo {

  public static final String ALARM_ID = "ALARM_ID";

  private String id;
  private String monitorId;
  private String monitorName;
  private Long ownerId;
  private String ownerType = "GROUP";
  private String dir;
  private AlarmType alarmType;
  private StringList alarmMsg;
  private String handler;
  private AlarmStatus status = AlarmStatus.UnResolved;
  private String comments;
  @JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  private Date expireTime;
  @JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  private Date createTime;
  @JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
  private Date updateTime;

  /**
   * 将{@link FileAlarmView}转换为{@link FileAlarmInfo}.
   */
  public static FileAlarmInfo valueOf(FileAlarmView view) {
    FileAlarmInfo alarmInfo = new FileAlarmInfo();
    alarmInfo.setId(view.getId());
    alarmInfo.setMonitorId(view.getMonitorId());
    alarmInfo.setMonitorName(view.getMonitorName());
    alarmInfo.setOwnerId(view.getOwnerId());
    alarmInfo.setOwnerType(view.getOwnerType());
    alarmInfo.setDir(view.getDir());
    alarmInfo.setAlarmType(AlarmType.fromString(view.getAlarmType()));
    StringList alarmMsg = new StringList();
    alarmMsg.setList(view.getAlarmMsg());
    alarmInfo.setAlarmMsg(alarmMsg);
    alarmInfo.setHandler(view.getHandler());
    alarmInfo.setStatus(view.getStatus());
    alarmInfo.setComments(view.getComments());
    alarmInfo.setExpireTime(view.getExpireTime());
    alarmInfo.setCreateTime(view.getCreateTime());
    alarmInfo.setUpdateTime(view.getUpdateTime());
    return alarmInfo;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getMonitorId() {
    return monitorId;
  }

  public void setMonitorId(String monitorId) {
    this.monitorId = monitorId;
  }

  public String getMonitorName() {
    return monitorName;
  }

  public void setMonitorName(String monitorName) {
    this.monitorName = monitorName;
  }

  public Long getOwnerId() {
    return ownerId;
  }

  public void setOwnerId(Long ownerId) {
    this.ownerId = ownerId;
  }

  public String getOwnerType() {
    return ownerType;
  }

  public void setOwnerType(String ownerType) {
    this.ownerType = ownerType;
  }

  public String getDir() {
    return dir;
  }

  public void setDir(String dir) {
    this.dir = dir;
  }

  public AlarmType getAlarmType() {
    return alarmType;
  }

  public void setAlarmType(AlarmType alarmType) {
    this.alarmType = alarmType;
  }

  public StringList getAlarmMsg() {
    return alarmMsg;
  }

  public void setAlarmMsg(StringList alarmMsg) {
    this.alarmMsg = alarmMsg;
  }

  public String getHandler() {
    return handler;
  }

  public void setHandler(String handler) {
    this.handler = handler;
  }

  public AlarmStatus getStatus() {
    return status;
  }

  public void setStatus(AlarmStatus status) {
    this.status = status;
  }

  public String getComments() {
    return comments;
  }

  public void setComments(String comments) {
    this.comments = comments;
  }

  public Date getExpireTime() {
    return expireTime;
  }

  public void setExpireTime(Date expireTime) {
    this.expireTime = expireTime;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getUpdateTime() {
    return updateTime;
  }

  public void setUpdateTime(Date updateTime) {
    this.updateTime = updateTime;
  }

  /**
   * 将{@link FileAlarmInfo}转换为{@link FileAlarmView}.
   */
  public FileAlarmView toView() {
    FileAlarmView view = new FileAlarmView();
    view.setId(this.getId());
    view.setMonitorId(this.getMonitorId());
    view.setMonitorName(this.getMonitorName());
    view.setOwnerId(this.getOwnerId());
    view.setOwnerType(this.getOwnerType());
    view.setDir(this.getDir());
    view.setAlarmType(this.getAlarmType().getMsg());
    view.setAlarmMsg(this.alarmMsg == null ? new ArrayList<>() : this.alarmMsg.getList());
    view.setHandler(this.getHandler());
    AlarmStatus status = this.getStatus();
    if (status == AlarmStatus.UnResolved
          && (this.getExpireTime() != null && this.getExpireTime().before(new Date()))) {
      status = AlarmStatus.ExpiredAndUnresolved;
    }
    view.setStatus(status);
    view.setComments(this.getComments());
    view.setExpireTime(this.getExpireTime());
    view.setCreateTime(this.getCreateTime());
    view.setUpdateTime(this.getUpdateTime());
    return view;
  }
}
